import { ElMessage, ElMessageBox, ElLoading } from 'element-plus';
import other from '/@/utils/other';
import { declareSubmit, declareSave, declareByIdAppDe, declareLocalFile } from '/@/api/digitalList/digitalLization';
//接口返回数据为null和带空格处理
export function isNull(data: any) {
	let newObje: any;
	newObje = data;
	if (data === null || data === ' ') {
		newObje = '';
	}
	return newObje;
	// return newObje.replace(/\s*/g, '');
}

export function formatTable(arr, pIdx) {
	for (let i = 1; i <= arr.length; i++) {
		let item = arr[i - 1];
		item.amount = item.amount || 0;
		if (pIdx == 0) {
			item.numIdx = i;
		} else {
			item.numIdx = pIdx + '.' + i;
		}
		item.parentNumIdx = pIdx;
		if (item.children) {
			item.children = formatTable(item.children, item.numIdx);
		}
	}
	return arr;
}
//资金预算明细数据-提交的时候转换结构
export function formatData(data: any) {
	data &&
		data.forEach((item: any) => {
			let _children = item.children;
			delete item.children;
			item['data'] = { ...item };
			delete item.amount;
			delete item.name;
			delete item.numIdx;
			delete item.parentNumIdx;
			delete item.remark;
			if (_children && _children.length > 0) {
				item['children'] = _children;
				formatData(_children);
			} else {
				item['children'] = [];
			}
		});
	return data;
}
//资金预算明细数据-获取的时候转换结构
export function formatDataList(data: any) {
	let list = [];
	data &&
		data.forEach((item: any) => {
			if (item.children && item.children.length) {
				list.push({
					...item.data,
					children: formatDataList(item.children),
				});
			} else {
				list.push({
					...item.data,
				});
			}
		});
	return list;
}

//文件上传校验
export function beforeUpload(file: any, accept: any) {
	const { size, type } = file;
	const fileType = accept.split(',');
	// const fileType = []
	if (fileType.length) {
		const fileName = file.name.split('.');
		const fileExt = `.${fileName[fileName.length - 1]}`;
		const isTypeOk = fileType.includes(fileExt);
		if (!isTypeOk) {
			ElMessage.error(`文件格式错误，请上传 ${fileType.join(',')} 格式的文件`);
			return false;
		}
	}
	const maxSize = 1024 * 1024 * 200; // 例如，限制为 200MB
	if (size > maxSize) {
		ElMessage.error('文件大小超过限制，请选择小于 200MB 的文件');
		return false;
	}
	return true;
}

//文件上传规格
export function fileList(index: number) {
	const list = [
		{
			propertyName: 'file1',
			label: '正式书面函',
			tips: '仅支持上传pdf/jpg格式文件，文件不能超过200MB ，图片文件不能超过1MB',
			data: [],
			accept: '.txt,.pdf,.png',
			type: '1',
		},
		{
			propertyName: 'file2',
			label: `资金计划报告（上传资金计划报告）`,
			labelWidth: '250',
			downText: '模板下载',
			tips: '仅支持上传doc/docx/pdf/jpg格式文件，文件不能超过200MB ，图片文件不能超过1MB',
			data: [],
			accept: '.jpg,.doc,.docx,.gif',
			type: 2,
		},
		{
			type: 3,
			propertyName: 'file3',
			label: '相关佐证材料',
			tips: '仅支持上传rar/zip/doc/docx/pdf/jpg格式的文件，并且文件大小不超过200M',
			data: [],
			accept: '.rar,.zip,.doc,.docx,.pdf,.jpg',
		},
		{
			tips: '仅支持上传rar/zip/doc/docx/pdf/jpg/xlsx格式的文件，并且文件大小不超过200M',
			data: [],
			accept: '.rar,.zip,.doc,.docx,.pdf,.jpg,.xlsx',
		},
		{
			tips: '仅支持上传pdf格式的文件，并且文件大小不超过200M',
			data: [],
			accept: '.pdf',
		},
	];
	return list[index];
}

//图片上传规格
export function fileImgList(index: number) {
	const list = [
		{
			data: ['.pdf', '.jpg', '.jpeg', '.png'],
		},
		{
			data: ['.pdf', '.jpg', '.jpeg', '.png', '.pptx'],
		},
	];
	return list[index].data;
}

//文件下载
export function downLoadFile(path: string) {
	other.downBlobFile(other.adaptationUrl(`/manage/local/file/${path}`), {}, `${path}`);
}

// 保存函数
export async function declareSaveList(data?: any) {
	const loading = ElLoading.service({
		lock: true,
		text: '提交中...',
	});
	await declareSave(data)
		.then((res) => {
			ElMessage({
				type: res.ok == true ? 'success' : 'error',
				message: res.data,
			});
		})
		.finally(() => {
			loading.close();
		});
}
